package com.catbad.services.impl;

import com.catbad.domain.User;
import com.catbad.mapper.UserMapper;
import com.catbad.services.UserServices;
import com.catbad.util.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServicesImpl implements UserServices {
    @Autowired
    UserMapper userMapper;
    @Override
    public int setUser(User user){
        //先获得密码，如果相同就不边，不同就加密修改
        String pass = userMapper.getPassById(user.getUid());
        if(!pass.equals(user.getUpass())){//不同将密码加密，作为修改
            try {
                user.setUpass(Tools.getMd5(user.getUpass()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
       return userMapper.updateByPrimaryKey(user);
    }

    @Override
    public Long getCinemaId(String account) {
        return userMapper.getCinemaId(account);
    }

    @Override
    public User login(String uaccount,String upass) {
        User user = userMapper.selectByUaccount(uaccount);
        //如果没有该用户
        if(user==null){
            return null;
        }
        //加密密码与数据库中的密码比较
        if(upass.equals(user.getUpass())){
            return user;
        }
        else{
            return null;
        }
    }

    @Override
    public int register(User user) {
       return userMapper.insert(user);
    }
}
